package evaluator

import (
	"lark/src/ast"
	"lark/src/object"
)

// 函数声明提升
func hoistFunctions(program *ast.Program, env *object.Environment) {

	for _, statement := range program.Statements {
		scanAST(statement, env)
	}

}

// 扫描 AST 语法树
func scanAST(node ast.Node, env *object.Environment) {
	if childNode, ok := node.(*ast.ExpressionStatement); ok {
		scanAST(childNode.Expression, env)
	} else if childNode, ok := node.(*ast.FunctionLiteral); ok {
		name := childNode.Name
		params := childNode.Parameters
		body := childNode.Body
		resultFunc := &object.Function{Name: name, Parameters: params, Env: env, Body: body}
		//放到符号表中
		// env.Set("func_"+name, resultFunc)
		env.Set("func_"+name, object.Symbol{
			Name: "func_" + name,
			// Value: nil, // 延迟计算
			Value: resultFunc, // 延迟计算
			Node:  nil,
		})
	}

}
